import { RouteConfigType } from "@/platform/types/route-config-type";
import { SwitchPropsType } from "@/platform/types/switch-props-type";
import React from "react";
import { Route, Switch } from "react-router-dom";

const renderRoutes = (routes:RouteConfigType, extraProps:any = {}, switchProps:SwitchPropsType = {}) => (routes ? (
    <Switch {...switchProps}>
        {
            routes.map((route:RouteConfigType, i:number) => (
                <Route
                    key={route.key || i}
                    path={route.path}
                    exact={route.exact}
                    strict={route.strict}
                    render={(props) => {
                        if (route.meta && route.meta.title) {
                            document.title = route.meta.title;
                        }

                        return (<route.component {...props} {...extraProps} route={route} />)
                    }}
                />
            ))
        }
    </Switch>
) : null);
export default renderRoutes;
